'use strict'
const util = require('util');
const request = require('request');
const {clarify} = require('./async_utility');

let requestPostPromise = util.promisify(request.post);
let requestGetPromise = util.promisify(request.get);
let requestPostClarified = clarify(util.promisify(request.post));
let requestGetClarified = clarify(util.promisify(request.get));
const moment = require('moment');

/**
 * 
 * @param {Date} date 
 */
function _time2Str (date) {
  if (date.getHours() >= 12) {
    return `${moment(date).format('YYYY-MM-DD')} 下午`
  } else {
    return `${moment(date).format('YYYY-MM-DD')} 上午`
  }
}
let date = new Date();
date.setHours(13);
console.log(_time2Str(date));
console.log(date)

module.exports = {
  _time2Str,
  requestPostPromise,
  requestGetPromise,
  requestPostClarified,
  requestGetClarified,
  /**
   * 执行 返回值为promise 的函数
   * @param {*} services 
   * @param {*} methodName 
   */
  runMethodByName (services, methodName) {
    const results = [];
    for (const item of services) {
      if (util.isFunction(item)) {
        results.push(item())
      } else if (util.isFunction(item[methodName])) {
        results.push(item[methodName]())
      }
    }
    return results
  }
}